/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.model;

import java.io.Serializable;
import java.util.Set;
import javax.jdo.annotations.Column;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * An operation is a number of Tasks that can be done on a field.
 * @author Merijn Zengers
 */
@NamedQueries(value = {
    @NamedQuery(name = Operation.Q_FIND_ALL_OPERATIONS, query = "select o from Operation o")
})
@Entity
@Table(name = "OPERATION")
public class Operation extends Bean implements Persistable, Serializable {
    public static final String Q_FIND_ALL_OPERATIONS = "findAllOperations";
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private long id;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PRODUCTION_UNIT_ID")
    private ProductionUnit productionUnit;
    
    @OneToMany(cascade=CascadeType.ALL, mappedBy="operation")
    private Set<Task> tasks;

    @Override
    public long getId() {
        return id;
    }
    
    public Set<Task> getTasks() {
        return tasks;
    }

    public ProductionUnit getProductionUnit() {
        return productionUnit;
    }

    public void setProductionUnit(ProductionUnit productionUnit) {
        ProductionUnit old = this.productionUnit;
        this.productionUnit = productionUnit;
        getPropertyChangeSupport().firePropertyChange("productionUnit", old, productionUnit);
        
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Operation other = (Operation) obj;
        if (this.id != other.id) {
            return false;
        }
        if (this.productionUnit != other.productionUnit && (this.productionUnit == null || !this.productionUnit.equals(other.productionUnit))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 37 * hash + (this.productionUnit != null ? this.productionUnit.hashCode() : 0);
        return hash;
    }


    
    /**
     * Operation builder class
     *
     * usage: Operation operation = new Operation.Builder().setName("someName").build();
     *
     */
    public static class Builder {

        private Operation operation;

        public Builder() {
            operation = new Operation();
        }

        public Builder setName(String name) {
            operation.setName(name);
            return this;
        }

        public Builder setProductionUnit(ProductionUnit productionUnit) {
            operation.setProductionUnit(productionUnit);
            return this;
        }
        
        public Builder addTask(Task task){
            operation.getTasks().add(task);
            return this;
        }

        public Operation build() {
            return operation;
        }
    }
}
